package com.tsite.dao.impl;

import javax.persistence.NoResultException;
import javax.persistence.StoredProcedureQuery;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import com.tsite.dao.IUserDAO;
import com.tsite.entity.User;

@Repository
public class UserDAO extends GenericDAO<User> implements IUserDAO {

	@Override
	public User selectUserById(Long id) {
		StoredProcedureQuery query = getEntityManager()
				.createNamedStoredProcedureQuery("selectUserById");
		query.setParameter("id", id);
		query.execute();
		User user = (User) query.getSingleResult();
		return user;
	}

	@Override
	public User selectUserByName(String userName) {
		User user = null;
		TypedQuery<User> query = getEntityManager().createNamedQuery("selectUserByName", User.class);
		query.setParameter("username", userName);
		try {
			user = query.getSingleResult();
		} catch (NoResultException e) {
			user = null;
		}
		return user;
	}

}